package com.ccrfid.rmc6.resource.device;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.appconfig.exception.pojo.CustomErrors;
import com.ccrfid.rmc6.entity.device.NetreaderEntity;
import com.ccrfid.rmc6.pojo.param.PageParam;
import com.ccrfid.rmc6.repository.search.Criterion;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.resource.device.pojo.FloorPos;
import com.ccrfid.rmc6.service.device.NetreaderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;

@Path("/netreaders")
@Produces(MediaType.APPLICATION_JSON)
public class NetreaderResource {
    private static final Logger logger = LoggerFactory.getLogger(NetreaderResource.class);
    @Autowired
    NetreaderService netreaderService;

    @GET
    public List<NetreaderEntity> getNetreaderList(@QueryParam("floorNumber") Integer floorNumber,
                                                  @QueryParam("pos") String pos) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (floorNumber != null) {
            criteria.add(new Criterion("floorNumber", Criterion.Operator.EQ, floorNumber));
        }
        if (pos != null) {
            if (pos.equalsIgnoreCase("Null")) {
                criteria.add(new Criterion("pos", Criterion.Operator.NULL, pos));
            } else if (pos.equalsIgnoreCase("NotNull")) {
                criteria.add(new Criterion("pos", Criterion.Operator.NOT_NULL, pos));
            }
        }
        List<NetreaderEntity> list = netreaderService.getNetreaderList(criteria, Sort.by(new ArrayList<>()));
        return list;
    }

    @GET
    @Path("search")
    public Page<NetreaderEntity> getNetreaderInfoPage(
            @QueryParam("readerNumber") Integer readerNumber,
            @QueryParam("ip") String ip,
            @QueryParam("onlineStatus") Integer onlineStatus,
            @QueryParam("floorNumber") Integer floorNumber,
            @QueryParam("pos") String pos,
            @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (readerNumber != null) {
            criteria.add(new Criterion("readerNumber", Criterion.Operator.EQ, readerNumber));
        }
        if (ip != null) {
            criteria.add(new Criterion("ip", Criterion.Operator.LIKE, '%' + ip + '%'));
        }
        if (onlineStatus != null) {
            criteria.add(new Criterion("onlineStatus", Criterion.Operator.EQ, onlineStatus));
        }
        if (floorNumber != null) {
            criteria.add(new Criterion("floorNumber", Criterion.Operator.EQ, floorNumber));
        }
        if (pos != null) {
            if (pos.equalsIgnoreCase("Null")) {
                criteria.add(new Criterion("pos", Criterion.Operator.NULL, pos));
            } else if (pos.equalsIgnoreCase("NotNull")) {
                criteria.add(new Criterion("pos", Criterion.Operator.NOT_NULL, pos));
            }
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }

        Page<NetreaderEntity> page = netreaderService.getNetreaderInfoPage(criteria, pageRequest);
        return page;
    }

    @GET
    @Path("{id}")
    public NetreaderEntity getNetreader(@PathParam("id") Long readerId) {
        NetreaderEntity netreaderEntity = netreaderService.getNetreader(readerId);
        return netreaderEntity;
    }

    @PUT
    @Path("{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    public NetreaderEntity updateNetreader(@PathParam("id") Long readerId, NetreaderEntity entity) throws AppException {
        NetreaderEntity netreaderEntity = netreaderService.updateNetreader(readerId, entity);
        return netreaderEntity;
    }

    @PUT
    @Path("{id}/position")
    @Consumes(MediaType.APPLICATION_JSON)
    public NetreaderEntity updatePosition(@PathParam("id") Long readerId, FloorPos floorPos) throws AppException {
        return netreaderService.updatePosition(readerId, floorPos);
    }

    @DELETE
    @Path("{id}/position")
    public Boolean deletePosition(@PathParam("id") Long readerId) throws AppException {
        return netreaderService.deletePosition(readerId);
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public NetreaderEntity createNetreader(NetreaderEntity entity) throws AppException {
        NetreaderEntity netreaderEntity = netreaderService.createNetreader(entity);
        return netreaderEntity;
    }

    @DELETE
    @Consumes(MediaType.APPLICATION_JSON)
    public int deleteNetreadersInfo(List<Long> idList) throws AppException {
        if (idList == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.param_is_null.getCode(), CustomErrors.param_is_null.getReason(), "");
        }
        return netreaderService.deleteNetreadersInfo(idList);
    }
}
